package com.atguigu.admin.service.impl;

import com.atguigu.admin.service.CategoryService;
import com.atguigu.clients.CategoryClient;
import com.atguigu.param.PageParam;
import com.atguigu.pojo.Category;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryClient categoryClient;// 微服务调用

    @Cacheable(value = "list.category",key = "#pageParam.currentPage+'-'+#pageParam.pageSize")
    @Override
    public R list(PageParam pageParam) {

        return categoryClient.listCategory(pageParam);
    }

    @CacheEvict(value = "list.category",allEntries = true)
    @Override
    public R save(Category category) {

        return categoryClient.save(category);
    }


    @Caching(
            evict = {
                    @CacheEvict(value = "list.category",allEntries = true),
                    @CacheEvict(value = "category",key = "#categoryId")
            }
    )
    @Override
    public R remove(Integer categoryId) {
        return categoryClient.remove(categoryId);
    }


    @Caching(
            evict = {
                    @CacheEvict(value = "list.category",allEntries = true),
                    @CacheEvict(value = "category",key = "#category.categoryId")
            }
    )
    @Override
    public R update(Category category) {
        return categoryClient.update(category);
    }
}
